import { Application } from 'egg';
import {
  indexRule,
  createRule,
  destroyRule,
  detailRule,
  updateRule,
  galleryRule,
  attrvalRule
} from '../validate/goods/goods';

/**
 * 用户组管理的路由
 */
export default (app: Application) => {
  const { auth } = app.middleware;
  const { jwt } = app;

  return {
    'goods.list': {
      method: 'GET',
      path: '/api/goods/list',
      options: {
        tags: ['goods/all'],
        description: '商品列表',
        handler: app.controller.goods.main.list,
        middlewares: [jwt, auth('goods.list')],
        validate: indexRule,
      }
    },
    'goods.create': {
      method: 'POST',
      path: '/api/goods',
      options: {
        tags: ['goods/create'],
        description: '创建商品',
        handler: app.controller.goods.main.create,
        middlewares: [jwt, auth('goods.list.create')],
        validate: createRule,
      },
    },
    'goods.update': {
      method: 'PUT',
      path: '/api/goods/:id',
      options: {
        tags: ['goods/update'],
        description: '更新商品',
        handler: app.controller.goods.main.update,
        middlewares: [jwt, auth('goods.list.update')],
        validate: updateRule,
      },
    },
    'goods.list.attrval': {
      method: 'POST',
      path: '/api/goods/attrval/:id',
      options: {
        tags: ['goods/attrval'],
        description: '创建商品sku',
        handler: app.controller.goods.main.attrval,
        middlewares: [jwt, auth('goods.list.attrval')],
        validate: attrvalRule,
      },
    },
    'goods.gallery': {
      method: 'DELETE',
      path: '/api/goods/gallery/:id',
      options: {
        tags: ['goods/gallery/destroy'],
        description: '删除商品详情的轮播图片',
        handler: app.controller.goods.main.goodsDestroy,
        middlewares: [jwt, auth('goods.list.gallery')],
        validate: galleryRule,
      },
    },
    'goods.detail': {
      method: 'GET',
      path: '/api/goods',
      options: {
        tags: ['goods/detail'],
        description: '商品详细信息',
        handler: app.controller.goods.main.detail,
        middlewares: [jwt, auth('goods.list.detail')],  // 小程序用的同一接口，去掉鉴权
        validate: detailRule,
      },
    },
    'goods.destroy': {
      method: 'DELETE',
      path: '/api/goods/goods/:id',
      options: {
        tags: ['goods/delete'],
        description: '删除商品',
        handler: app.controller.goods.main.destroy,
        middlewares: [jwt, auth('goods.list.destroy')],
        validate: destroyRule,
      },
    },
  };
};
